﻿
///////////////////////////////////////////////////////////////////////////////////
/// 文 件 名: ReimbursementExpensesForm.cs
/// 作    者: XXD
/// 创建日期: 2012/6/18 9:48:18
/// 
///---------------------------------修改记录--------------------------------------
/// 修 改 人:          
/// 修改日期:
/// 修改原因: 
//////////////////////////////////////////////////////////////////////////////////
namespace ZX.WebApp.WorkFlow.CustomForms
{
    using System;
    using ZX.BPM.Library;
    using ZX.Web.UI.Controls;
    using DevExpress.Web.ASPxEditors;

    /// <summary>
    /// (借款，需求，报销)报销单页面
    /// </summary>
    public partial class ReimbursementExpensesForm : EditUIPage
    {
        #region 页面加载事件

        protected void Page_Load(object sender, EventArgs e)
        {
            NeedsOfBorrowerEdit entity =null;
            if (this.Session["User"] == null)
            {
                Response.Redirect("../../Login.aspx");
            }
            if (!this.IsPostBack)
            {
                this.Session["NeedsOfBorrower" + strWorkflowInsId] = null;

                entity = GetNeedsOfBorrowerEdit();
               
                //设置数据源
                NeedsOfBorrower.Bind(entity, true);
                NeedsOfBorrower.DataBind();

                //设置报销信息
                ReimbursementExpensesInfo.Bind(entity, true);
                ReimbursementExpensesInfo.DataBind();
            }
            else
            {
                entity = GetNeedsOfBorrowerEdit();
            }
         

          
            NeedsOfBorrower.InitFileUploader(entity);
            MaterialsProcurementInfo.Bind(entity, true);
            MaterialsProcurementInfo.DataBind();


            //设置采购单信息
            MaterialsProcurementDetail.DataSource = entity;
            MaterialsProcurementDetail.DataBind();

            this.ReimbursementExpensesDetail.DataSource = entity;
            this.ReimbursementExpensesDetail.DataBind();

            //设置只读
            NeedsOfBorrower.ReadOnly = true;
            MaterialsProcurementInfo.ReadOnly = true;
            MaterialsProcurementDetail.ReadOnly = true;

            if (Status == "3")
            {
                ReimbursementExpensesInfo.ReadOnly = true;
                ReimbursementExpensesDetail.ReadOnly = true;
                ApprovalControl.ReadOnly = true;
                btnSubmit.Enabled = false;
                btnReturn.Enabled = false;
            }

        }

        #endregion

        #region 事件处理

        public void btnSubmit_Click(object sender, EventArgs e)
        {
            NeedsOfBorrowerEdit data = (NeedsOfBorrowerEdit)this.Session["NeedsOfBorrower" + strWorkflowInsId];
            if (data == null)
            {
                return;
            }

            try
            {

                ReimbursementExpensesInfo.Bind(data, false);
                ReimbursementExpensesDetail.EndEdit();

                //保存数据
                SaveNeedsOfBorrower(data);

                ApprovalControl.SaveUserControlApp(
                   this.User.LoginName,
                   this.User.UName,
                   this.EmpPropertyValues.DeptName,
                   this.EmpPropertyValues.PostName,
                   strOperatorInstanceId,
                   strWorkFlowId,
                   strWorkflowInsId,
                   strWorkTaskId,
                   strWorkTaskInsId);

                ASPxButton button = (ASPxButton)sender;
                ApprovalControl.Submit(
                    this.User.LoginName,
                    strOperatorInstanceId,
                    button.Text);

                //跳转到提示页面
                Response.Redirect(@"../../Crm/BasePages/tooltip.aspx?WorkTaskInstanceId=" + ApprovalControl.GetRedirectID(strWorkTaskInsId, strOperatorInstanceId));
                Response.Close();
            }
            catch (Exception ex)
            {
                ZX.BLL.Extend.LogBusiness.WriteExceptionLog(ex, this.User);
            }
        }

        #endregion

        #region 查询参数

        /// <summary>
        /// 
        /// </summary>
        public string strWorkFlowId
        {
            get
            {
                string wtid = this.GetStringFromQueryString("WorkflowId", "");
                if (wtid == "")
                {
                    wtid = this.GetStringFromQueryString("wfid", "");
                   // return "3ae48d7c-f539-4e56-a8a8-3abe8e0eebf1";
                }
                return wtid;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskId
        {
            get
            {
                string wfid = this.GetStringFromQueryString("WorktaskId", "");
                if (wfid == "")
                {
                    wfid = this.GetStringFromQueryString("wtid", "");
                    //return "50cd4b83-8e90-47bf-a9e5-484c529ecefa";
                }
                return wfid;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        public string strOperatorInstanceId
        {
            get
            {
               return this.GetStringFromQueryString("OperatorInstanceId", "");
               // return "";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkflowInsId
        { 
            get { 
              return this.GetStringFromQueryString("WorkflowInsId", ""); 
                //return "4dc695e9-336e-452c-bc44-54920f48ce2b";
            } 
        }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskInsId
        { 
            get {
                return this.GetStringFromQueryString("WorktaskInsId", "");
               // return "7548bb26-4737-45ec-8935-b2245fd50b2a";
            } 
        }

        public string strView
        { get { return this.GetStringFromQueryString("view", ""); } }
        public string strOperStatus
        { get { return this.GetStringFromQueryString("OperStatus", ""); } }
        public string strDescript
        { get { return this.GetStringFromQueryString("descript", ""); } }

        public string Status
        { get { return this.GetStringFromQueryString("statues", ""); } }

        #endregion


        #region 本地方法

        private void SaveNeedsOfBorrower(NeedsOfBorrowerEdit needsOfBorrower)
        {
            try
            {
                Session["NeedsOfBorrower" + strWorkflowInsId] = needsOfBorrower.Save();
            }
            catch (Csla.Validation.ValidationException ex)
            {
                System.Text.StringBuilder message = new System.Text.StringBuilder();
                message.AppendFormat("{0}", ex.Message);
                if (needsOfBorrower.BrokenRulesCollection.Count > 0)
                {
                    message.Append("<ul>");
                    foreach (Csla.Validation.BrokenRule rule in needsOfBorrower.BrokenRulesCollection)
                        message.AppendFormat("<li>{0}: {1}</li>", rule.Property, rule.Description);
                    message.Append("</ul>");
                }
            }
            catch (Csla.DataPortalException ex)
            {
            }
            catch (Exception ex)
            {
            }
        }

        private NeedsOfBorrowerEdit GetNeedsOfBorrowerEdit()
        {
            NeedsOfBorrowerEdit businessObject = (NeedsOfBorrowerEdit)Session["NeedsOfBorrower" + strWorkflowInsId];
            if (businessObject == null)
            {
                try
                {
                    if (!string.IsNullOrEmpty(strWorkflowInsId))
                    {
                        businessObject = NeedsOfBorrowerEdit.Get(strWorkflowInsId);
                        //businessObject.WorkTaskInsId = strWorkTaskInsId;
                        //businessObject.WorkTaskId = strWorkTaskId;
                    }
                    else
                    {
                        businessObject = NeedsOfBorrowerEdit.New();
                        businessObject.CreatedTime = DateTime.Now.ToShortDateString();
                        businessObject.Proposer = base.UserName;
                        businessObject.PostName = base.EmpPropertyValues.PostName;
                        businessObject.Filiale = base.EmpPropertyValues.Filiale;
                        businessObject.DeptName = base.EmpPropertyValues.DeptName;
                    }
                    Session["NeedsOfBorrower" + strWorkflowInsId] = businessObject;
                }
                catch (System.Security.SecurityException)
                {

                }
            }

            return businessObject;
        }

        #endregion
    }
}